var industry = new Object();
industry.businessIds = [];
var nodess=[];

industry.init = function(){
    industry.getData();
    industry.initBusinessTree();
}

industry.getData = function () {
    $("#business_select2").select2({
        tags:true,
        data: business_json,
        multiple:true,
        placeholder:"请选择范围",
        language:"zh-CN"
    });
}

industry.initBusinessTree = function () {
    // 加载树
    industry.business_zTree = $.fn.zTree.init($("#group_tree"), {
        check: {
            enable: true,
            chkboxType :  { "Y" : "s", "N" : "s" }
            //nocheckInherit: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            dblClickExpand: false
        },
        callback: {
            onCheck: function (event, treeId, treeNode) {
                var nodes = industry.business_zTree.getCheckedNodes(true);
                industry.businessIds = [];
                var businessName = [];
                var n;
                for(var i = 0; i < nodes.length; i++){
                    var n = nodes[i];
                    if(n.pId == null){
                        industry.businessIds = [];
                        industry.businessIds.push(n.id);
                        businessName = [];
                        businessName.push(n.name);
                        $("#business_select2").val(industry.businessIds).trigger("change");
                        if(nodes.length > 1){
                            industry.cancelNode(industry.business_zTree, n);
                        }
                        $("#businessId").val(industry.businessIds.join(","));
                        $("#businessName").val(businessName.join(","));
                        return;
                    }
                    industry.businessIds.push(n.id);
                    businessName.push(n.name);
                }
                $("#businessId").val(industry.businessIds.join(","));
                $("#businessName").val(businessName.join(","));
                //前台显示并查询
                $("#business_select2").val(industry.businessIds).trigger("change");
                // industry.deleteDept();
            }
        }
    }, business_tree);

    //点击小X触发，取消树中节点的勾选状态
    industry.deleteDept = function () {
        $(document).delegate('.select2-selection__choice__remove','click',function(){
            var id = $(this).parent().attr('id');
            var treeObj = $.fn.zTree.getZTreeObj("group_tree");
            var node = treeObj.getNodeByParam("id", id, null);
            node.checked = false ;
            treeObj.updateNode(node);
            var checkedNodes = treeObj.getCheckedNodes(true);
            var businessName = [];
            industry.businessIds = [];
            if(checkedNodes.length > 0){
                var businessName = [];
                industry.businessIds = [];
                if(id == '1'){  //全部行业
                    treeObj.checkAllNodes(false);
                }else{
                    for(var i=0; i< checkedNodes.length; i++) {
                        var n = checkedNodes[i];
                        industry.businessIds.push(n.id);
                        businessName.push(n.name);
                    }
                }
            }
            $("#businessId").val(industry.businessIds.join(","));
            $("#businessName").val(businessName.join(","));
        })
    }

}


industry.findBusiness=function(businessId) {
    var businessIds1 =new Array();
    businessIds1 =businessId.split(',')
    $("#business_select2").val(businessIds1).trigger("change");
}

industry.selectBusiness=function(){
    //得到最新的受训单位数据
    var businesses = $("#business_select2").select2("data");
    industry.getNewDate(businesses);
    $('.pos').removeClass('tree_containenr');

};

industry.getNewDate=function(data){
    var businessData = [];
    for(var i = 0;i<data.length;i++){
        businessData.push(data[i].id);
    }
    industry.businessIds = businessData;
}


function mergeOption(pid) {

    var opt = '<option value="">请选择</option>';
    for(var i=0; i<resion_json.length; i++){
        var temp = resion_json[i];
        if(temp.pId == pid){
            opt += '<option value="'+temp.id+'">'+temp.text+'</option>';
        }
    }
    return opt;
}

function mergeOption11(pid) {
    var opt = "";
    for(var i=0; i<resion_json.length; i++){
        var temp = resion_json[i];
        if(temp.id == pid){
            opt += temp.text;
        }
    }
    return opt;
}

function mergeOption1(pid) {
    var opt='';
    for(var i=0; i<resion_json.length; i++){
        var temp = resion_json[i];
        if(temp.id == pid){
            opt += '<option value="'+temp.id+'">'+temp.text+'</option>';
        }
    }
    return opt;
}

function findPid(id) {
    for(var i=0; i<resion_json.length; i++){
        var temp = resion_json[i];
        if(temp.id == id){
            var pid=temp.pId;
        }
    }
    return pid;
}
function selectProvince(pid) {
    $("#province").html(mergeOption(pid));
}

function selectCity(pid) {
    $("#city").html(mergeOption(pid));
}

function selectDistrict(pid) {
    $("#district").html(mergeOption(pid));
}

function selectProvince1(pid) {
    $("#province").html(mergeOption1(pid));
}

function selectCity1(pid) {
    $("#city").html(mergeOption1(pid));
}

function selectDistrict1(pid) {
    $("#district").html(mergeOption1(pid));
}

function saveCompanyInfo(){
    var companyName = $("#companyName").val();
    var province = $("#province").val();
    var city = $("#city").val();
    var district = $("#district").val();
    var businessIds = $("#business_select2").val().toString();
    var companyCode = $("#socialCreditCode").val();
    if(companyName == ""){
        layer.msg("请输入企业名称");
        return;
    }
    if(province == ""){
        layer.msg("请输入省份");
        return;
    }
    if(city == ""){
        layer.msg("请输入市名称");
        return;
    }
    if(district == ""){
        layer.msg("请输入区名称");
        return;
    }
    if(businessIds == ""){
        layer.msg("请输入行政区划");
        return;
    }
    if (isCompany()!=1){
        return;
    }

    var param = {
        'companyName':companyName,
        'regionName':district,
        'businessIds':businessIds,
        'companyCode':companyCode
    }
    $.ajax({
        url:appPath+ '/popup/saveCompanyinfo' ,
        async: true,
        type: 'post',
        data: param,
        success: function(data){
            var result = data['code'];
            if(result == '10000'){
                layer.alert('操作成功');
            }else{
                layer.alert(result.message)
            }
        }
    });
}

function isCompany(){
    var n=1;
    var companyCode = $("#socialCreditCode").val();
    var param = {
        'companyCode':companyCode
    }
    $.ajax({
        url:appPath+ '/popup/isCompany' ,
        async: true,
        type: 'post',
        data: param,
        success: function(data){
            var result = data['code'];
            if(result == '10000'){
                n=1;
            }else{
                n=2;
                layer.alert('该社会信用代码已存在')
            }
        }
    });
    return n;
}

function register(){
    var companyName = $("#companyName").val();
    var province = $("#province").val();
    var city = $("#city").val();
    var district = $("#district").val();
    //var industryType = $("#industryType").val();
    var industryType = $("#businessId").val();
    var socialCreditCode = $("#socialCreditCode").val();
    if(companyName == ""){
        layer.msg("请输入企业名称");
        return;
    }
    if(province == ""){
        layer.msg("请输入省份");
        return;
    }
    if(city == ""){
        layer.msg("请输入市");
        return;
    }
    if(industryType == ""){
        layer.msg("请输入行业类别");
        return;
    }
    var regionName = "";
    if(district != ""){
        regionName +=  district+"#"+mergeOption11(district);
    }else{
        regionName +=  city+"#"+mergeOption11(city);
    }

    var param = {
        'companyName':companyName,
        'regionName':regionName,
        'industryType':industryType,
        'socialCreditCode':socialCreditCode
    }
    $.ajax({
        url: appPath+ '/popup/register',
        type:'post',
        dataType: 'json',
        data:param,
        success:function (data) {
            if(data && data['code'] == '50503'){
                layer.msg("登录超时!");
                return;
            } else if(data && data['code'] == '70001'){
                layer.msg(data['message']);
            }else {
                layer.msg("注册新单位的申请已发送，请等待审核");
            }
        }
    })
}

//返回
function back() {
    window.close();
    /* var url = appPath +  "/popup/companyChange";
     //跳转到注册页面
     window.location.href = url;*/
}
